package com.aiyige.utils;

import android.app.Activity;
import android.net.Uri;

import com.aiyige.R;
import com.aiyige.configs.MyConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.tools.AttrsUtils;
import com.vondear.rxtools.RxFileTool;
import com.yalantis.ucrop.UCrop;

import java.io.File;


public class ImageCropper {
    public static final String IMAGE_CROP_DIR = MyConfig.AIYIGE_ROOT_DIR + ".imagecrop" + File.separator;
    public static final String NO_MEDIA_FILE = IMAGE_CROP_DIR + ".nomedia";

    boolean circleDimmedLayer;
    boolean hideBottomControls;
    boolean freeStyleCropEnabled;
    boolean showCropFrame;
    boolean showCropGrid;
    float aspectRatioX;
    float aspectRatioY;
    Activity activity;
    int cropWidth;
    int cropHeight;
    String path;
    int cropCompressQuality;

    private ImageCropper(Builder builder) {
        circleDimmedLayer = builder.circleDimmedLayer;
        hideBottomControls = builder.hideBottomControls;
        freeStyleCropEnabled = builder.freeStyleCropEnabled;
        showCropFrame = builder.showCropFrame;
        showCropGrid = builder.showCropGrid;
        aspectRatioX = builder.aspectRatioX;
        aspectRatioY = builder.aspectRatioY;
        activity = builder.activity;
        cropWidth = builder.cropWidth;
        cropHeight = builder.cropHeight;
        path = builder.path;
        cropCompressQuality = builder.cropCompressQuality;
    }

    public static Builder newBuilder() {
        return new Builder();
    }


    public void startCrop() {
        RxFileTool.createOrExistsDir(IMAGE_CROP_DIR);
        RxFileTool.createOrExistsFile(NO_MEDIA_FILE);
        UCrop.Options options = new UCrop.Options();
        int toolbarColor = AttrsUtils.getTypeValueColor(activity, R.style.picture_default_style, R.attr.picture_crop_toolbar_bg);
        int statusColor = AttrsUtils.getTypeValueColor(activity, R.style.picture_default_style, R.attr.picture_crop_status_color);
        int titleColor = AttrsUtils.getTypeValueColor(activity, R.style.picture_default_style, R.attr.picture_crop_title_color);
        options.setToolbarColor(toolbarColor);
        options.setStatusBarColor(statusColor);
        options.setToolbarWidgetColor(titleColor);
        options.setCircleDimmedLayer(circleDimmedLayer);
        options.setShowCropFrame(showCropFrame);
        options.setShowCropGrid(showCropGrid);
        options.setCompressionQuality(cropCompressQuality);
        options.setHideBottomControls(hideBottomControls);
        options.setFreeStyleCropEnabled(freeStyleCropEnabled);
        boolean isHttp = PictureMimeType.isHttp(path);
        String imgType = PictureMimeType.getLastImgType(path);
        Uri uri = isHttp ? Uri.parse(path) : Uri.fromFile(new File(path));
        UCrop.of(uri, Uri.fromFile(new File(IMAGE_CROP_DIR + UUIDUtil.generate() + imgType)))
                .withAspectRatio(aspectRatioX, aspectRatioY)
                .withMaxResultSize(cropWidth, cropHeight)
                .withOptions(options)
                .start(activity);
    }

    public static final class Builder {
        private boolean circleDimmedLayer;
        private boolean hideBottomControls;
        private boolean freeStyleCropEnabled;
        private boolean showCropFrame;
        private boolean showCropGrid;
        private float aspectRatioX;
        private float aspectRatioY;
        private Activity activity;
        private int cropWidth;
        private int cropHeight;
        private String path;
        private int cropCompressQuality;

        private Builder() {
            circleDimmedLayer = false;
            hideBottomControls = true;
            freeStyleCropEnabled = false;
            showCropFrame = true;
            showCropGrid = false;
            aspectRatioX = 0;
            aspectRatioY = 0;
            cropWidth = 0;
            cropHeight = 0;
            path = "";
            cropCompressQuality = 100;
        }

        public Builder circleDimmedLayer(boolean val) {
            circleDimmedLayer = val;
            return this;
        }

        public Builder hideBottomControls(boolean val) {
            hideBottomControls = val;
            return this;
        }

        public Builder freeStyleCropEnabled(boolean val) {
            freeStyleCropEnabled = val;
            return this;
        }

        public Builder showCropFrame(boolean val) {
            showCropFrame = val;
            return this;
        }

        public Builder showCropGrid(boolean val) {
            showCropGrid = val;
            return this;
        }

        public Builder aspectRatioX(float val) {
            aspectRatioX = val;
            return this;
        }

        public Builder aspectRatioY(float val) {
            aspectRatioY = val;
            return this;
        }

        public Builder activity(Activity val) {
            activity = val;
            return this;
        }

        public Builder cropWidth(int val) {
            cropWidth = val;
            return this;
        }

        public Builder cropHeight(int val) {
            cropHeight = val;
            return this;
        }

        public Builder path(String val) {
            path = val;
            return this;
        }

        public Builder cropCompressQuality(int val) {
            cropCompressQuality = val;
            return this;
        }

        public ImageCropper build() {
            return new ImageCropper(this);
        }
    }
}
